package cn.monkey.chat.server.connect.chat.state.group;

import cn.monkey.state.core.StateGroup;
import cn.monkey.state.core.reactive.SimpleReactiveStateGroupFactory;
import reactor.core.publisher.Mono;

import java.util.function.Supplier;

public class ChatGroupFactory extends SimpleReactiveStateGroupFactory {

    private final ChatRepository chatRepository;

    public ChatGroupFactory(Supplier<Long> timer, ChatRepository chatRepository) {
        super(timer);
        this.chatRepository = chatRepository;
    }


    @Override
    public Mono<StateGroup> create(String id, Object... args) {
        return this.chatRepository.findById(id)
                .map(chatContext -> {
                    ChatGroup chatGroup = new ChatGroup(id, chatContext, this.timer);
                    chatGroup.addState(new ChatState(chatGroup));
                    chatGroup.setStartState(ChatState.CODE);
                    return chatGroup;
                });
    }
}
